﻿using DoNet.Helper;
using DoNet.Platform.RenRen;

namespace DoNet.OAuth2
{
    /// <summary>
    /// 人人网授权登录
    /// </summary>
    public class OAuthRenRen : OAuthBase
    {
        /// <summary>
        /// 平台
        /// </summary>
        public override OAuthServer Platform
        {
            get
            {
                return OAuthServer.renren;
            }
        }

        /// <summary>
        /// 获取Authorization Code 的URL
        /// </summary>
        /// <returns></returns>
        public override string GetAuthorizeURL()
        {
            return TokenApi.GetAuthorizeUrl(AppKey, CallbackUrl, "renren");
        }

        /// <summary>
        /// 获取是否通过授权。
        /// </summary>
        public override bool Authorize()
        {
            if (!string.IsNullOrEmpty(code))
            {
                var result = AccessToken.GetAccessToken(AppKey, AppSecret, code, CallbackUrl);
                if (result != null)
                {
                    if (result.success)
                    {
                        var res = JsonHelper.DeserializeObject<dynamic>(result.data);
                        if (res != null)
                        {
                            token = res.access_token;
                            expiresTime = Convert.ToDateTime(res.expires_time);

                            openID = res.id;
                            nickName = res.nickName;
                            headUrl = res.headUrl;
                        }
                    }
                }
            }
            return false;
        }
    }
}
